<?php
import('$/util/hash_type.php');
/**
 * 分类信息处理
 */
class CateController extends Controller
{
	function show()
	{
		$pid	=	fetch_get('pid','int');
		if($pid)
		{
			$this->assign('path',	Cate::getPathCate($pid));
			$this->assign('pid',	$pid);
		}
		
		$list	=	Cate::getByPid($pid);

		$this->assign('list',		$list);
		$this->assign('node_types',	node_type_list());
		$this->display();
	}

	function add()
	{
		$pid	=	fetch_get('pid','int');
		$parent	=	null;
		if($pid)
		{
			$parent	=	Cate::get($pid);
			if(!$parent){
				$this->redirect('cate');
			}
		}
		
		if(!empty($_POST))
		{
			if(Cate::add(array(
				'title'		=>	FilterHelper::strict(fetch_post('title')),
				'desc'		=>	FilterHelper::strict(fetch_post('desc')),
				'pid'		=>	$pid,
				'type'		=>	$parent ? $parent['type'] : fetch_post('type','int'),
			))){
				Show::notice('添加成功', 'cate/pid/'.$pid);
			}
			else{
				Show::warning('添加失败');
			}
		}

		$this->assign('node_types',	node_type_list());
		$this->assign('parent',		$parent);
		$this->display();
	}

	function mod()
	{
		$id		=	fetch_get('id', 'int');
		$cate	=	Cate::get($id);
		if(!$cate)
		{
			Show::warning('no such cate', 'cate');
		}
		
		if(!empty($_POST))
		{
			if(Cate::modify($id, array(
				'title'		=>	FilterHelper::strict(fetch_post('title')),
				'desc'		=>	FilterHelper::strict(fetch_post('desc')),
			))){
				Show::notice('修改成功', 'cate/pid/'.$cate['pid']);
			}
			else{
				Show::warning('修改失败', 'cate-mod/id/'.$id);
			}
		}
		
		$this->assign('cate',	$cate);
		$this->assign('node_types',	node_type_list());
		$this->display();
	}
	
}
?>